package com.dormitory.management.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.req.LossPageReq;
import com.dormitory.management.system.req.LossPersonalPageReq;
import com.dormitory.management.system.req.LossSaveReq;
import com.dormitory.management.system.req.LossUpdateReq;
import com.dormitory.management.system.resp.LossResp;
import com.dormitory.management.system.service.LossService;
import com.dormitory.managemnet.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 遗失物品表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "遗失物品管理")
@RestController
@RequestMapping("/loss")
public class LossController {

    @Autowired
    private LossService lossService;

    @ApiOperation("遗失和寻物上报")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<String> save(@RequestBody LossSaveReq req){
        if(!lossService.save(req)) {
            return Result.error("上报失败！");
        }
        return Result.success("上报成功！");
    }

    @ApiOperation("图片上传")
    @RequestMapping(value = "/saveFile",method = RequestMethod.POST)
    public Result<String> saveFile(@RequestParam("file") MultipartFile file) throws IOException {
        return Result.success(lossService.saveFile(file));
    }

    @ApiOperation("删除启示")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<String> delete(
            @ApiParam(value = "启示id",required = true)
            @PathVariable Long id){
        if(!lossService.delete(id)) {
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("修改启示内容")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<String> update(@RequestBody LossUpdateReq req){
        if(!lossService.update(req)) {
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }


    @ApiOperation("遗物启示状态更新")
    @RequestMapping(value = "/updateStatus/{id}",method = RequestMethod.GET)
    public Result<String> updateStatus(
            @ApiParam(value = "启示id",required = true)
            @PathVariable Long id){
        if(!lossService.updateStatus(id)) {
            return Result.error("启示已完结，不可更新！");
        }
        return Result.success("状态更新成功！");
    }

    @ApiOperation("遗忘启示分页列表（公共）")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<LossResp>> getPage(@RequestBody LossPageReq req){
        return Result.ok(lossService.getPage(req));
    }

    @ApiOperation("遗忘启示分页列表（个人）")
    @RequestMapping(value = "/getPersonalPage",method = RequestMethod.POST)
    public Result<IPage<LossResp>> getPersonalPage(@RequestBody LossPersonalPageReq req){
        return Result.ok(lossService.getPersonalPage(req));
    }
}
